package com.xihuanjun.healthcode.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.xihuanjun.healthcode.config.SysCache;
import com.xihuanjun.healthcode.entity.ChangeRecord;
import com.xihuanjun.healthcode.entity.convert.ChangeRecordConvert;
import com.xihuanjun.healthcode.entity.dto.ChangeRecordDTO;
import com.xihuanjun.healthcode.service.ChangeRecordService;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 江溪浣
 * @createDate: 2022/10/26 10:35
 * @description:
 */
@Slf4j
public class ExcelRowListener implements ReadListener<ExcelRow> {
    private ChangeRecordConvert changeRecordConvert;
    /**
     * 读取的工作表名字，红码/橙码/绿码
     */
    private String sheetName;
    /**
     * 入库用
     */
    ChangeRecordService changeRecordService;
    /**
     * 读完之后的操作类型，空则入库；
     * red则替换red缓存
     * orange则替换orange缓存
     * green则替换green缓存
     */
    private String operateType="";

    public ExcelRowListener(ChangeRecordConvert convert,String sheetName,ChangeRecordService service){
        this.changeRecordConvert=convert;
        this.sheetName=sheetName;
        this.changeRecordService=service;
    }

    public ExcelRowListener(ChangeRecordConvert changeRecordConvert, String sheetName, ChangeRecordService changeRecordService, String operateType) {
        this.changeRecordConvert = changeRecordConvert;
        this.sheetName = sheetName;
        this.changeRecordService = changeRecordService;
        this.operateType = operateType;
    }

    //存储转换后的每条数据
    List<ChangeRecordDTO> recordList=new ArrayList<>();
    //存储转换过程中出错的记录
    List<String> resultMsg=new ArrayList<>();
    int index=1;

    /**
     * When analysis one row trigger invoke function.
     *
     * @param data    one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context analysis context
     */
    @Override
    public void invoke(ExcelRow data, AnalysisContext context) {
        try{
            switch (this.operateType)
            {
                case "red":
                    data.setChangeType(1);
                    break;

                case "orange":
                    data.setChangeType(2);
                    break;

                case "green":
                    data.setChangeType(3);
                    break;
            }
            log.debug("{},{}",sheetName,data);
            ChangeRecordDTO record = changeRecordConvert.excel2DTO(data);
//            if(sheetName.equals("红码")){
//                record.setChangeType(1);
//            }else if(sheetName.equals("橙码")){
//                record.setChangeType(2);
//            }else{
//                record.setChangeType(3);
//            }
            recordList.add(record);
            log.debug("转换成功，加入到list");
        }
        catch (Exception e){
            log.debug("转换失败");
            e.printStackTrace();
            String message="";
            if(e.getMessage().contains("Unparseable date")){
                message="日期格式错误";
            }
            resultMsg.add(MessageFormat.format("第{0}行有错，姓名：{1}，错误：{2}",index,data.getName(),message));
        }
        index++;
    }

    /**
     * if have something to do after all analysis
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.debug("{}数据读取完毕",sheetName);
        if(resultMsg.size()==0){
            log.debug("没有错误");
            if(this.operateType.equals("")){
                //TODO:批量入库
            }else{
                //插入到全局底库缓存
                switch (this.operateType){
                    case "red":
                        SysCache.red=recordList;
                        break;
                    case "orange":
                        SysCache.orange=recordList;
                        break;
                    case "green":
                        SysCache.green=recordList;
                        break;
                }
            }
        }else{
            resultMsg.stream().forEach(log::debug);
        }
    }
}
